package app.web.galgame.collect;

import app.common.dao.Leo_galgame_infoBean;

import app.common.dao.Leo_res_infoBean;
import app.esb.common.FileTools;
import cn.lily.com.component.widget.standard.TWForm;
import cn.lily.com.db.DBFactory;
import cn.lily.com.db.container.reader.DataTable;
import cn.lily.com.exception.YaconException;
import cn.lily.com.lang.Basic;
import cn.lily.com.lang.BusinessDate;
import cn.lily.com.lang.number.MathUtil;

import cn.lily.esb.enums.EnumManager;
import cn.lily.web.page.BusinessPage;
import cn.lily.web.widget.sql.TSqlGrid;
import cn.lily.web.widget.standard.TButton;
import cn.lily.web.widget.standard.TButtonPilot;
import cn.lily.web.widget.standard.TDrop;
import cn.lily.web.widget.standard.TInput;
/**
 * GalGame采集处理
 * @author JiaJ
 * @date 2018年5月31日
 *
 */
public class GalGameCollect extends BusinessPage {

	@Override
	public void page_onLoad() throws YaconException {
		
		TSqlGrid galGameGrid = sqlGrid("galGameGrid");
		galGameGrid.setDictTable("LEO_GALGAME_INFO");
		
		String sqlString = "select RES_ID,GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,"
								+ "GAME_VISUAL,GAME_SIZE,GAME_SIZE_NUM,GAME_FACTORY,GAME_FIRST,"
								+ "GAME_INTRU,GEME_MEMO,GAME_NAME_PLUS,"
								+ "DISK_ID,GAME_STORE_PATH,GAME_TAGS,"
								+ "to_char(GAME_PRO_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_PRO_TIME,"
								+ "to_char(GAME_CRE_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_CRE_TIME "
						 + "from ${schema}.LEO_GALGAME_INFO "
						 + " order by GAME_CRE_TIME desc ";
		
		galGameGrid.setCommSqlStr(sqlString);
		galGameGrid.setEdit(true);
		
		galGameGrid.createFieldTitle("GAME_NAME").setFieldAlign("center").setDataAlign("center");
		galGameGrid.createFieldTitle("GAME_TYP").setFieldAlign("center").setDataAlign("center");
		galGameGrid.createFieldTitle("GAME_SIZE").setFieldAlign("center").setDataAlign("center");
		galGameGrid.createFieldTitle("GAME_PRO_TIME").setFieldAlign("center").setDataAlign("center");
		galGameGrid.createFieldTitle("GAME_TAGS").setFieldAlign("center").setDataAlign("center");
		galGameGrid.createFieldTitle("RES_ID").setFieldVisible(false);
		galGameGrid.createFieldTitle("GAME_ID").setFieldVisible(false);
		galGameGrid.createFieldTitle("GAME_LANG").setFieldVisible(false);		
		galGameGrid.createFieldTitle("GAME_VISUAL").setFieldVisible(false);		
		galGameGrid.createFieldTitle("GAME_SIZE_NUM").setFieldVisible(false);
		galGameGrid.createFieldTitle("GAME_FACTORY").setFieldVisible(false);
		galGameGrid.createFieldTitle("GAME_FIRST").setFieldVisible(false);		
		galGameGrid.createFieldTitle("GAME_INTRU").setFieldVisible(false);
		galGameGrid.createFieldTitle("GEME_MEMO").setFieldVisible(false);
		galGameGrid.createFieldTitle("GAME_NAME_PLUS").setFieldVisible(false);
		galGameGrid.createFieldTitle("DISK_ID").setFieldVisible(false);
		galGameGrid.createFieldTitle("GAME_STORE_PATH").setFieldVisible(false);	
		galGameGrid.createFieldTitle("GAME_CRE_TIME").setFieldVisible(false);
		
		//页面路径相对与入口页面，注意，这里的无宽高处理方式
		galGameGrid.detailPage().setPagePath("galgameEdit.jsp");
		//导航部分
		TButtonPilot galGamePilot = ButtonPilot("galGamePilot");
		galGamePilot.setPilotIco("/widget/buttonPilot.gif");
		galGamePilot.setPilotText("GALGAME信息");

		galGamePilot.addButtons(new TButton("add"));
		galGamePilot.addButtons(new TButton("edit"));
		galGamePilot.addButtons(new TButton("delete"));

		
		//数据分析
		TButton dataAnalyseBtn = new TButton("dataAnalyseBtn");
		dataAnalyseBtn.setLabel("数据拆分");
		dataAnalyseBtn.setViewClick("dataAnalyseBtn_click");
		dataAnalyseBtn.setState("warning");
		dataAnalyseBtn.setIcoPath("glyphicon glyphicon-cloud");
		galGamePilot.addButtons(dataAnalyseBtn);
		
		galGameGrid.setButtonPilot(galGamePilot);
		
		galGameGrid.setTopButtonType("all");
		
		this.setFindForm(galGameGrid.createFindForm());

		getPage().addElement(galGameGrid);
		
	}
	
	public void refresh(){
		
		TSqlGrid galGameGrid = sqlGrid("galGameGrid");
		galGameGrid.setDictTable("LEO_GALGAME_INFO");
		
		String sqlString = "select RES_ID,GAME_ID,GAME_NAME,GAME_TYP,GAME_LANG,"
								+ "GAME_VISUAL,GAME_SIZE,GAME_SIZE_NUM,GAME_FACTORY,GAME_FIRST,"
								+ "GAME_INTRU,GEME_MEMO,GAME_NAME_PLUS,"
								+ "DISK_ID,GAME_STORE_PATH,GAME_TAGS,"
								+ "to_char(GAME_PRO_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_PRO_TIME,"
								+ "to_char(GAME_CRE_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_CRE_TIME "
						 + "from ${schema}.LEO_GALGAME_INFO "
						 + "where 1=1 "+this.setWhereSQL()
						 +" order by GAME_CRE_TIME desc";
		
		galGameGrid.setCommSqlStr(sqlString);
		
		this.setSelfMessage(galGameGrid.getDataElement());
		
	}
	
	public void save(){
		
		Leo_galgame_infoBean galgameBean = new Leo_galgame_infoBean();
		
		while (getViewTable().next()) {
			
			galgameBean.setViewRecord(getViewTable().getRecord());
			
			if (getViewTable().getRecord().getRecordType() == 1) {
			

				galgameBean.setGame_id(System.currentTimeMillis()+""+MathUtil.random(3)+"");

				galgameBean.setGame_cre_time(BusinessDate.getTodaytime());
				galgameBean.setGame_pro_time(BusinessDate.formatDate(this.getViewTable().getString("GAME_PRO_TIME"))+" 00:00:00");
				
				String size = this.getViewTable().getString("GAME_SIZE");
				if("".equals(size) || size == null){
					size = FileTools.getFileSize(this.getViewTable().getString("GAME_STORE_PATH"));
				}else{
					size = "0MB";
				}
				galgameBean.setGame_size(size);
				galgameBean.setGame_store_path(FileTools.getRelaLocation(this.getViewTable().getString("GAME_STORE_PATH")));

				String gameMemo = this.getViewTable().getString("GAME_INTRU");
				if(gameMemo.length()>1100){
					gameMemo = gameMemo.substring(0,1100) + "..."; 
				}
				galgameBean.setGame_intru(gameMemo);
				
				galgameBean.insert();				
			}
			
			else if (getViewTable().getRecord().getRecordType() == 2) {

				String gameMemo = this.getViewTable().getString("GAME_INTRU");
				if(gameMemo.length()>1100){
					gameMemo = gameMemo.substring(0,1100) + "..."; 
				}
				galgameBean.setGame_intru(gameMemo);

				galgameBean.updateByWhere("where GAME_ID='"+getViewTable().getString("GAME_ID")+"'");
			}
			
			else if (getViewTable().getRecord().getRecordType() == 3) {
			
				if(this.getViewTable().getString("RES_ID") != null && !"".equals(this.getViewTable().getString("RES_ID"))){
					this.dataRelaDelete("LEO_RES_INFO", "RES_ID");
				}
							
				galgameBean.deleteByWhere("where GAME_ID='"+getViewTable().getString("GAME_ID")+"'");
			}
		}
		
		this.setSuccess("数据操作成功！");
		
	}
	
	private void setFindForm(TWForm findForm) {
		
		TInput gameName = Input("gameName");
		gameName.setLabel("游戏名称");
		
		TDrop gameType = Drop("gameType");
		gameType.put("", "");
		gameType.setEnumDetail(EnumManager.getTableEnum("GAME_TYP"));
		gameType.setLabel("游戏类型");
		
		TDrop gameDate = Drop("gameDate", TDrop.FIELD_DROP_DATE);
		gameDate.setLabel("游戏发售时间");
		
		TInput gameTags = Input("gameTags");
		gameTags.setLabel("游戏标签");
		
		// 查询
		TButton find = Button("selectBtn");
		find.setIcoPath("glyphicon glyphicon-search");
		find.setViewClick("galGameGrid_selectBtn_onclick");
		find.setLabel("查询");
		// 重置
		TButton reset = Button("resetBtn");
		reset.setIcoPath("glyphicon glyphicon-share");
		reset.setViewClick("galGameGrid_resetBtn_onclick");
		reset.setLabel("重置");

		// Form 排版
		findForm.createFormCell(gameName,6);
		findForm.createFormCell(gameType,5);
		findForm.createFormCell(find,1);
		
		findForm.createFormRow();
		
		findForm.createFormCell(gameDate,6);
		findForm.createFormCell(gameTags,5);	
		findForm.createFormCell(reset,1);
	}
	
	public String setWhereSQL() {
		
		String ret = " ";
		
		String gameName = this.getViewTable().getAttributeValue("gameName");
		String gameType = this.getViewTable().getAttributeValue("gameType");
		String gameDate = this.getViewTable().getAttributeValue("gameDate");
		String gameTags = this.getViewTable().getAttributeValue("gameTags");
		
		if (gameName != null && !"".equals(gameName)) {
			ret += " and ( upper(GAME_NAME) like '" + Basic.toWhereLike(gameName.trim().toUpperCase()) + "' or upper(GAME_NAME_PLUS) like '" + Basic.toWhereLike(gameName.trim().toUpperCase()) + "') ";
		}
		
		if (gameType != null && !"".equals(gameType)) {
			ret += " and GAME_TYP like '%"+gameType+"%' ";
		}
		
		if (gameDate != null && !"".equals(gameDate)) {
			ret += " and GAME_PRO_TIME like '"+gameDate.substring(0,7)+"%' ";
		}
		
		if (gameTags != null && !"".equals(gameTags)) {
			
			String[] tags = gameTags.split(",");
			
			for(String tag:tags){

				ret += " and GAME_TAGS like '"+Basic.toWhereLike(tag.trim().toUpperCase())+"' ";

			}
					
		}
		
		return ret;
	}
	

	public void dataAnalyse(){
		
		try {
					
			String sql = "select GAME_NAME,GAME_SIZE,GAME_STORE_PATH,GAME_ID,"
					          + "to_char(GAME_CRE_TIME,'yyyy-mm-dd hh24:mi:ss') GAME_CRE_TIME,"
					          + "GAME_CRE_TIME,DISK_ID,GAME_TAGS "
					   + "from ${schema}.LEO_GALGAME_INFO where RES_ID is null or RES_ID='' ";
			
			DataTable galTable = DBFactory.getActionDB().executeQueryTable(sql);
			while(galTable.next()){
				
				try {
					
					System.out.println(galTable.getString("GAME_NAME")+"开始处理.......");
					
					String resID = System.currentTimeMillis()+MathUtil.random(3);
					
					Leo_res_infoBean resBean = new Leo_res_infoBean();
					resBean.setRes_id(resID);
					resBean.setRes_nam(galTable.getRecord().getStringNotNull("GAME_NAME"));
					resBean.setRes_typ("10");
					resBean.setRes_store_size(galTable.getRecord().getStringNotNull("GAME_SIZE"));
					resBean.setRes_store_place(galTable.getRecord().getStringNotNull("GAME_STORE_PATH"));
					resBean.setRes_time(galTable.getRecord().getStringNotNull("GAME_CRE_TIME"));
					resBean.setRes_oper("9999");
					resBean.setDisk_id(galTable.getRecord().getStringNotNull("DISK_ID"));
					resBean.setRes_tag_memo(galTable.getRecord().getStringNotNull("GAME_TAGS"));
					
					resBean.insert();
					
					DBFactory.getActionDB().executeUpdate("update ${schema}.LEO_GALGAME_INFO set RES_ID='"+resID+"' where GAME_ID='"+galTable.getString("GAME_ID")+"' ");
					
				} catch (Exception e) {
					e.printStackTrace();
					continue;
				}		
				
			}
			
			this.setSuccess("数据拆分成功！");
			
		} catch (Exception e) {
			e.printStackTrace();
			throw new YaconException("数据拆分失败！");
		}
		
	}

}
